package com.dongdongsongcan.app.control;

import com.dongdongsongcan.app.common.DDMessageDef;
import com.dongdongsongcan.app.common.DDSpKeysDef;
import com.dongdongsongcan.app.control.bean.AddressData;
import com.dongdongsongcan.app.control.bean.AuthnumData;
import com.dongdongsongcan.app.control.bean.DDLocation;
import com.dongdongsongcan.app.control.bean.DDLocationData;
import com.dongdongsongcan.app.control.bean.UserData;
import com.dongdongsongcan.app.control.bean.UserFavorableData;
import com.dongdongsongcan.app.db.UserDao;
import com.dongdongsongcan.app.db.bean.User;
import com.dongdongsongcan.app.model.api.WebServiceApi;
import com.su.dolphin.control.BaseControl;
import com.su.dolphin.control.bean.BaseBean;
import com.su.dolphin.control.task.CtrlTask;
import com.su.dolphin.control.task.CtrlTask.IControlResponse;
import com.su.dolphin.control.task.CtrlTask.IControlWork;
import com.su.dolphin.core.notification.DpMessageCenter;
import com.su.dolphin.utils.DataStoreUtil;
import com.su.dolphin.utils.JSONUtil;
import com.su.dolphin.utils.ui.TipsUtil;

public class UserControl extends BaseControl {

    private static UserControl sControl;
    private User mUser;
    private static AddressData.Address mDefaultAddress; //用户选择的送货地址

    public boolean userExist() {
        return getUser() != null;
    }

    public User getUser() {
        if (mUser == null) {
            mUser = UserDao.getUser();
        }
        return mUser;
    }

    public void logout() {
        if (mUser != null)
            UserDao.deleteUser(mUser);
        mUser = null;
    }

    public void saveUser(User user) {
        if (UserDao.saveUser(user)) {
            DpMessageCenter.i().notify(new DpMessageCenter.MCMessage(DDMessageDef.N_USER_CHANGE));
            mUser = user;
        } else {
            TipsUtil.toast("更新User失败!");
        }

    }

    public static UserControl i() {
        if (sControl == null) {
            sControl = new UserControl();
        }
        return sControl;
    }


    public void authnum(final String user_mobile,
                        IControlResponse<AuthnumData> response) {
        CtrlTask.submitTask(new IControlWork() {

            @Override
            public BaseBean doWork() {
                AuthnumData authnumData = WebServiceApi.authnum(user_mobile);
                return authnumData;
            }
        }, response);

    }

    public void user_favorable(final int shopId, IControlResponse<UserFavorableData> response) {
        CtrlTask.submitTask(new IControlWork() {

            @Override
            public UserFavorableData doWork() {
                UserFavorableData authnumData = WebServiceApi.user_favorable(shopId);
                return authnumData;
            }
        }, response);

    }

    public void mobile_login(final String user_mobile, final String authnum,
                             final String session_id, IControlResponse<UserData> response) {
        CtrlTask.submitTask(new IControlWork() {

            @Override
            public BaseBean doWork() {
                UserData authnumData = WebServiceApi.mobile_login(
                        user_mobile, authnum, session_id);
                return authnumData;
            }
        }, response);

    }

    public void register_mobile(final String user_mobile, final String authnum,
                                final String session_id, IControlResponse<UserData> response) {
        CtrlTask.submitTask(new IControlWork() {

            @Override
            public BaseBean doWork() {
                UserData authnumData = WebServiceApi.register_mobile(
                        user_mobile, authnum, session_id);
                return authnumData;
            }
        }, response);

    }

    public void user_login(final String user_name, final String user_pwd,
                           IControlResponse<UserData> response) {
        CtrlTask.submitTask(new IControlWork() {

            @Override
            public BaseBean doWork() {
                UserData authnumData = WebServiceApi.user_login(user_name,
                        user_pwd);
                return authnumData;
            }
        }, response);

    }

    public AddressData.Address getDefaultAddress() {
        if (mDefaultAddress == null) {
            mDefaultAddress = JSONUtil.parseObject(DataStoreUtil.i().getString(DDSpKeysDef.KEY_DEFAULT_ADDRESS, ""), AddressData.Address.class);
        }
        return mDefaultAddress;

    }

    public void saveDefaultAddress(AddressData.Address address) {
        mDefaultAddress = address;
        String strAddress = JSONUtil.toJSONString(address);
        DataStoreUtil.i().putString(DDSpKeysDef.KEY_DEFAULT_ADDRESS, strAddress);
        DpMessageCenter.i().notify(new DpMessageCenter.MCMessage(DDMessageDef.N_SUBMIT_ORDER_CHOOSADDRESS, address));
    }
}
